<?php
/**
* View the history of changes for a single page
*
* @package folio
* @param string $user The user/community to which this page belongs.
* @param string $page The title of the page being shown.
*/
    define("context", "folio_page_history");
    // Run includes
    require("../../../includes.php");
    require_once("../view/page.php");
    require_once("../model/user.php");

    // Retrieve passed variables (page name & user name, as well as created).
    //     These variables will be used by the menu system to build links.  See lib.php for more details.
    //    Note that the included side menu depends upon these variable names.
    $_title = folio_page::title_decode(required_param('page'));
    $_username = folio_clean( required_param('user') );
    $url = url;

    // Load Page information
    $page = folio_page::SelectWhereTitleAndUsername( $_title, $_username );
    if( !$page ) {
        // Page hasn't been created yet.
        trigger_error('You can not call the history page for a page that has not yet been created.', E_USER_ERROR);
    }

    // Don't use these vars, default to the ones in $page now.
    unset( $_username ); unset( $_title);

    // Load User Information
    $user = folio_user::SelectWhereIdent( $page->user_ident );
    if ( !$user ) {
        trigger_error("Invalid username $user->username passed to view.php", E_USER_ERROR);
    }
    // set the current user.
    global $page_owner;
    $page_owner = $user->ident;


    // Test permissions.
    if( !$page->Permission('select') ) {
        // No permission.
        $html_title = __gettext('History');
        $plain_title = $html_title;
        $body = __gettext('You do not have permission to view this page. Please contact the page\'s' .
                ' owner and ask for the security to be set to Public or Moderated. ' .
                '  You will be able to view this page once that has been done.  If this page belongs ' .
                ' to a community, you may also try to join the community.  Once you are a member, ' .
                ' you will be able to see and edit the page.');

    } else {
        // Permission.

        // Build nice titles
        $html_title = "<a href='{$url}{$user->username}/subscribe/rss/page+page_comment/'>" .
                    "<img border=0 src='{$url}mod/folio/image/feed.png' /></a>" .
                    $user->name . ' : ' .
                    folio_PageBreadcrumb::html($page);

        $plain_title = $page->title . ' ' . __gettext("History");

        $children = $page->SelectHistory();

        $html = '';
        $user_cache = array();

        if( count($children) > 0 ) {
            // Create a link for each version.
            foreach( $children as $p ) {

                // Load user information for that edit (if needed).
                if ( !isset($user_cache[$page->creator_ident] )) {
                    // Load from db.
                    $user_cache[$page->creator_ident] = folio_user::SelectWhereIdent($page->creator_ident);
                }

                // build date
                $date = date( $FOLIO_CFG->gmstyle, intval( $p->created) );

                // Build link.
                $html .= "<li><a href='{$url}{$user->username}/page/{$p->page_ident}:{$p->created}'>" .
                    "{$p->title}</a> by <a href='{$url}" .
                    $user_cache[$p->creator_ident]->username . "'>" .
                    $user_cache[$p->creator_ident]->name . "</a>" .
                    " on {$date}";
            }
        } else {
            // No old versions.
        }

    }

    // Transfer into template & write.
    templates_page_setup();
    $body = templates_draw(array(
                    'context' => 'contentholder',
                    'title' => $html_title,
                    'body' => $html
                ));

    echo templates_page_draw(array(
                    $plain_title, $html
                ));

?>
